package mysgoog.pattern.dragon;

import java.io.*;
import java.util.*;

import mysgoog.gtp.Vertex;
import mysgoog.pattern.Storage.ValuePair;
import mysgoog.pattern.Storage.VertexList;
/**
* @author Fu, Zhongyang
*/
public class ConnPatternLoader
{

	static int c = 0;
    private static Map<Integer, Map<String, VertexList>> mapList = null;  

    public static void load(Map<Integer, Map<String, VertexList>> map) throws FileNotFoundException, IOException, ClassNotFoundException
    {   	
    	mapList = map;
        read("pattern\\DragonPattern.dbpattern");
    }

    public static void read(String filename) throws FileNotFoundException, IOException
    {
        BufferedReader reader = null;
        try
        {
            File file = new File(filename);
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null)
            {
                parse(text);
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    private static void parse(String pattern)
    {
        String[] token = pattern.split(" ");
        int num = Integer.parseInt(token[0]) * 13 + Integer.parseInt(token[1]);
        VertexList list = new VertexList(token[2]);
        //System.out.println(token[2]);
        for (int i = 4; i < token.length; i += 2){
        	list.list.add(new Vertex(Integer.parseInt(token[i]), Integer.parseInt(token[i+1])));
        }
        generatePattern(num, token[3], list);
    }
    
    private static void generatePattern(int num, String pattern, VertexList value){
		generatePatterno(num, pattern, value);
    }
    
    private static void generatePatterno(int num, String pattern, VertexList value){
		String newPattern = pattern.replaceFirst("o", "O");
		if (!pattern.equals(newPattern)){
			generatePatterno(num, newPattern, value);
			newPattern = pattern.replaceFirst("o", ".");
			generatePatterno(num, newPattern, value);
		} else		
		generatePatternx(num, pattern, value);
    }
    private static void generatePatternx(int num, String pattern, VertexList value){
		String newPattern = pattern.replaceFirst("x", "X");
		if (!pattern.equals(newPattern)){
			generatePattern(num, newPattern, value);
			newPattern = pattern.replaceFirst("x", ".");
			generatePattern(num, newPattern, value);
		} else
		generatePatternq(num, pattern, value);
    }
    private static void generatePatternq(int num, String pattern, VertexList value){
		String newPattern = pattern.replaceFirst("[?]", ".");
		if (!pattern.equals(newPattern)){
			generatePattern(num, newPattern, value);
			newPattern = pattern.replaceFirst("[?]", "X");
			generatePattern(num, newPattern, value);
			newPattern = pattern.replaceFirst("[?]", "O");
			generatePattern(num, newPattern, value);
		} else 
		{
			//System.out.println(c++ + " " + value.type);
			putInMap(num, pattern, value);   		
		}   		
    }
    private static void putInMap(int num, String pattern, VertexList value){
        Map<String, VertexList> map;
        if (mapList.containsKey(num)) map = mapList.get(num);
        else{
        	map = new HashMap<String, VertexList>();
        	mapList.put(num, map);
        }
    	map.put(pattern, value);
    	
    }
    
}
